# Copyright (c) 2007 NTmatter
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

DESCRIPTION="Learning version of Side Effects Software Inc's Houdini suite."

DEPEND="x11-base/xorg-x11 app-shells/tcsh"
HOMEPAGE="http://www.sidefx.com"
IUSE=""
KEYWORDS="~x86 ~amd64"
LICENSE="Side Effects Software Inc"
RESTRICT="fetch nomirror nostrip"
SLOT=0

if use amd64; then
	arch_string="x86_64_deb_4.0"
else
	arch_string="rhEL4"
fi

file_base="houdini-${PV}-linux_${arch_string}"
At="${file_base}.tar.gz"

SRC_URI="amd64? ( houdini-${PV}-linux_x86_64_deb_4.0.tar.gz )
         x86? ( houdini-${PV}-linux_rhEL4.tar.gz)"

pkg_nofetch() {
	einfo "Please register at sidefx.com and download ${At}"
	use amd64 && einfo "(Debian 64-bit Linux) and move it to ${DISTDIR}"
	use x86 && einfo "(Red Hat Enterprise Linux 4) and move it to ${DISTDIR}"
}

src_unpack() {
	einfo "Extracting outer houdini package..."
	unpack ${A}

	einfo "Extracting inner houdini package..."
	cd ${WORKDIR}/${file_base}/
}

src_install() {
	HFS=/opt/hfs-${PV}
	
	einfo "Extracting Houdini files to image directory..."
	dodir ${HFS}
	tar --no-same-permissions --no-same-owner -xzf ${WORKDIR}/${file_base}/houdini.tar.gz -C ${D}${HFS}
	einfo "Finished extraction."

	# Make a home for the license files
	dodir /usr/lib/sesi

	# Create script to start/stop license server
	dodir /etc/init.d
	cat ${FILESDIR}/sesinetd-template.sh \
	| sed -e "s/REPLACE-ME-WITH-HFS/\\/opt\\/hfs-${PV}/g" \
	> ${D}etc/init.d/sesinetd

	chmod 755 ${D}etc/init.d/sesinetd
	chown root:root ${D}etc/init.d/sesinetd
	
	# Create convenience scripts for calling houdini programs
	dodir /usr/bin
	for i in mplay hhalo hhalo-ng hkey hmaster-ng houdini hselect vcc; do
		cat ${FILESDIR}/houdini-template.sh \
		| sed -e "s/REPLACE-ME-WITH-HFS/\\/opt\\/hfs-${PV}/g" \
	    | sed -e "s/REPLACE-ME-WITH-PROGRAM/${i}/g" \
		> ${D}/usr/bin/${i};
		chmod 755 ${D}/usr/bin/${i}
		chown root:root ${D}/usr/bin/${i}
	done
}

pkg_prerm() {
	einfo "Shutting down Houdini License Server"
	/etc/init.d/sesinetd stop
}
